home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / var / lib / dpkg / info / libx11-6.preinst < prev    next >
Encoding:
Text File  |  2007-04-04  |  29.8 KB  |  927 lines

  1. #!/bin/sh
  2. set -e
  3.  
  4. THIS_PACKAGE=libx11-6
  5. THIS_SCRIPT=preinst
  6.  
  7. # $Id: xsfbs.sh 2888 2006-08-22 03:04:13Z dnusinow $
  8.  
  9. # This is the X Strike Force shell library for X Window System package
  10. # maintainer scripts.  It serves to define shell functions commonly used by
  11. # such packages, and performs some error checking necessary for proper operation
  12. # of those functions.  By itself, it does not "do" much; the maintainer scripts
  13. # invoke the functions defined here to accomplish package installation and
  14. # removal tasks.
  15.  
  16. # If you are reading this within a Debian package maintainer script (e.g.,
  17. # /var/lib/dpkg)info/PACKAGE.{config,preinst,postinst,prerm,postrm}), you can
  18. # skip past this library by scanning forward in this file to the string
  19. # "GOBSTOPPER".
  20.  
  21. SOURCE_VERSION=2:1.1.1-1ubuntu3
  22. OFFICIAL_BUILD=
  23.  
  24. # Use special abnormal exit codes so that problems with this library are more
  25. # easily tracked down.
  26. SHELL_LIB_INTERNAL_ERROR=86
  27. SHELL_LIB_THROWN_ERROR=74
  28. SHELL_LIB_USAGE_ERROR=99
  29.  
  30. # old -> new variable names
  31. if [ -z "$DEBUG_XORG_PACKAGE" ] && [ -n "$DEBUG_XFREE86_PACKAGE" ]; then
  32.   DEBUG_XORG_PACKAGE="$DEBUG_XFREE86_PACKAGE"
  33. fi
  34. if [ -z "$DEBUG_XORG_DEBCONF" ] && [ -n "$DEBUG_XFREE86_DEBCONF" ]; then
  35.   DEBUG_XORG_DEBCONF="$DEBUG_XFREE86_DEBCONF"
  36. fi
  37.  
  38. # initial sanity checks
  39. if [ -z "$THIS_PACKAGE" ]; then
  40.   cat >&2 <<EOF
  41. Error: package maintainer script attempted to use shell library without
  42. definining \$THIS_PACKAGE shell variable.  Please report the package name,
  43. version, and the text of this error message to the Debian Bug Tracking System.
  44. Visit <http://www.debian.org/Bugs/Reporting> on the World Wide Web for
  45. instructions, read the file /usr/share/doc/debian/bug-reporting.txt from the
  46. "doc-debian" package, or install the "reportbug" package and use the command of
  47. the same name to file a report against version $SOURCE_VERSION of this package.
  48. EOF
  49.   exit $SHELL_LIB_USAGE_ERROR
  50. fi
  51.  
  52. if [ -z "$THIS_SCRIPT" ]; then
  53.   cat >&2 <<EOF
  54. Error: package maintainer script attempted to use shell library without
  55. definining \$THIS_SCRIPT shell variable.  Please report the package name,
  56. version, and the text of this error message to the Debian Bug Tracking System.
  57. Visit <http://www.debian.org/Bugs/Reporting> on the World Wide Web for
  58. instructions, read the file /usr/share/doc/debian/bug-reporting.txt from the
  59. "doc-debian" package, or install the "reportbug" package and use the command of
  60. the same name to file a report against version $SOURCE_VERSION of the
  61. "$THIS_PACKAGE" package.
  62. EOF
  63.   exit $SHELL_LIB_USAGE_ERROR
  64. fi
  65.  
  66. ARCHITECTURE="$(dpkg --print-installation-architecture)"
  67.  
  68. LAPTOP=""
  69. if [ -n "$(which laptop-detect)" ]; then
  70.     if laptop-detect >/dev/null; then
  71.     LAPTOP=true
  72.     fi
  73. fi
  74.  
  75. if [ "$1" = "reconfigure" ] || [ -n "$DEBCONF_RECONFIGURE" ]; then
  76.   RECONFIGURE="true"
  77. else
  78.   RECONFIGURE=
  79. fi
  80.  
  81. if ([ "$1" = "install" ] || [ "$1" = "configure" ]) && [ -z "$2" ]; then
  82.   FIRSTINST="yes"
  83. fi
  84.  
  85. if [ -z "$RECONFIGURE" ] && [ -z "$FIRSTINST" ]; then
  86.   UPGRADE="yes"
  87. fi
  88.  
  89. trap "message;\
  90.       message \"Received signal.  Aborting $THIS_PACKAGE package $THIS_SCRIPT script.\";\
  91.       message;\
  92.       exit 1" HUP INT QUIT TERM
  93.  
  94. reject_nondigits () {
  95.   # syntax: reject_nondigits [ operand ... ]
  96.   #
  97.   # scan operands (typically shell variables whose values cannot be trusted) for
  98.   # characters other than decimal digits and barf if any are found
  99.   while [ -n "$1" ]; do
  100.     # does the operand contain anything but digits?
  101.     if ! expr "$1" : "[[:digit:]]\+$" > /dev/null 2>&1; then
  102.       # can't use die(), because it wraps message() which wraps this function
  103.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_nondigits() encountered" \
  104.            "possibly malicious garbage \"$1\"" >&2
  105.       exit $SHELL_LIB_THROWN_ERROR
  106.     fi
  107.     shift
  108.   done
  109. }
  110.  
  111. reject_whitespace () {
  112.   # syntax: reject_whitespace [ operand ]
  113.   #
  114.   # scan operand (typically a shell variable whose value cannot be trusted) for
  115.   # whitespace characters and barf if any are found
  116.   if [ -n "$1" ]; then
  117.     # does the operand contain any whitespace?
  118.     if expr "$1" : "[[:space:]]" > /dev/null 2>&1; then
  119.       # can't use die(), because I want to avoid forward references
  120.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_whitespace() encountered" \
  121.            "possibly malicious garbage \"$1\"" >&2
  122.       exit $SHELL_LIB_THROWN_ERROR
  123.     fi
  124.   fi
  125. }
  126.  
  127. reject_unlikely_path_chars () {
  128.   # syntax: reject_unlikely_path_chars [ operand ... ]
  129.   #
  130.   # scan operands (typically shell variables whose values cannot be trusted) for
  131.   # characters unlikely to be seen in a path and which the shell might
  132.   # interpret and barf if any are found
  133.   while [ -n "$1" ]; do
  134.     # does the operand contain any funny characters?
  135.     if expr "$1" : '.*[!$&()*;<>?|].*' > /dev/null 2>&1; then
  136.       # can't use die(), because I want to avoid forward references
  137.       echo "$THIS_PACKAGE $THIS_SCRIPT error: reject_unlikely_path_chars()" \
  138.            "encountered possibly malicious garbage \"$1\"" >&2
  139.       exit $SHELL_LIB_THROWN_ERROR
  140.     fi
  141.     shift
  142.   done
  143. }
  144.  
  145. # Query the terminal to establish a default number of columns to use for
  146. # displaying messages to the user.  This is used only as a fallback in the
  147. # event the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while
  148. # the script is running, and this cannot, only being calculated once.)
  149. DEFCOLUMNS=$(stty size 2> /dev/null | awk '{print $2}') || true
  150. if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" > /dev/null 2>&1; then
  151.   DEFCOLUMNS=80
  152. fi
  153.  
  154. message () {
  155.   # pretty-print messages of arbitrary length
  156.   reject_nondigits "$COLUMNS"
  157.   echo "$*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS} >&2
  158. }
  159.  
  160. observe () {
  161.   # syntax: observe message ...
  162.   #
  163.   # issue observational message suitable for logging someday when support for
  164.   # it exists in dpkg
  165.   if [ -n "$DEBUG_XORG_PACKAGE" ]; then
  166.     message "$THIS_PACKAGE $THIS_SCRIPT note: $*"
  167.   fi
  168. }
  169.  
  170. warn () {
  171.   # syntax: warn message ...
  172.   #
  173.   # issue warning message suitable for logging someday when support for
  174.   # it exists in dpkg; also send to standard error
  175.   message "$THIS_PACKAGE $THIS_SCRIPT warning: $*"
  176. }
  177.  
  178. die () {
  179.   # syntax: die message ...
  180.   #
  181.   # exit script with error message
  182.   message "$THIS_PACKAGE $THIS_SCRIPT error: $*"
  183.   exit $SHELL_LIB_THROWN_ERROR
  184. }
  185.  
  186. internal_error () {
  187.   # exit script with error; essentially a "THIS SHOULD NEVER HAPPEN" message
  188.   message "internal error: $*"
  189.   if [ -n "$OFFICIAL_BUILD" ]; then
  190.     message "Please report a bug in the $THIS_SCRIPT script of the" \
  191.             "$THIS_PACKAGE package, version $SOURCE_VERSION to the Debian Bug" \
  192.             "Tracking System.  Include all messages above that mention the" \
  193.             "$THIS_PACKAGE package.  Visit " \
  194.             "<http://www.debian.org/Bugs/Reporting> on the World Wide Web for" \
  195.             "instructions, read the file" \
  196.             "/usr/share/doc/debian/bug-reporting.txt from the doc-debian" \
  197.             "package, or install the reportbug package and use the command of" \
  198.             "the same name to file a report."
  199.   fi
  200.   exit $SHELL_LIB_INTERNAL_ERROR
  201. }
  202.  
  203. usage_error () {
  204.   message "usage error: $*"
  205.   message "Please report a bug in the $THIS_SCRIPT script of the" \
  206.           "$THIS_PACKAGE package, version $SOURCE_VERSION to the Debian Bug" \
  207.           "Tracking System.  Include all messages above that mention the" \
  208.           "$THIS_PACKAGE package.  Visit " \
  209.           "<http://www.debian.org/Bugs/Reporting> on the World Wide Web for" \
  210.           "instructions, read the file" \
  211.           "/usr/share/doc/debian/bug-reporting.txt from the doc-debian" \
  212.           "package, or install the reportbug package and use the command of" \
  213.           "the same name to file a report."
  214.   exit $SHELL_LIB_USAGE_ERROR
  215. }
  216.  
  217.  
  218. maplink () {
  219.   # returns what symlink should point to; i.e., what the "sane" answer is
  220.   # Keep this in sync with the debian/*.links files.
  221.   # This is only needed for symlinks to directories.
  222.   #
  223.   # XXX: Most of these look wrong in the X11R7 world and need to be fixed.
  224.   # If we've stopped using this function, fixing it might enable us to re-enable
  225.   # it again and catch more errors.
  226.   case "$1" in
  227.     /etc/X11/xkb/compiled) echo /var/lib/xkb ;;
  228.     /etc/X11/xkb/xkbcomp) echo /usr/X11R6/bin/xkbcomp ;;
  229.     /usr/X11R6/lib/X11/app-defaults) echo /etc/X11/app-defaults ;;
  230.     /usr/X11R6/lib/X11/fs) echo /etc/X11/fs ;;
  231.     /usr/X11R6/lib/X11/lbxproxy) echo /etc/X11/lbxproxy ;;
  232.     /usr/X11R6/lib/X11/proxymngr) echo /etc/X11/proxymngr ;;
  233.     /usr/X11R6/lib/X11/rstart) echo /etc/X11/rstart ;;
  234.     /usr/X11R6/lib/X11/twm) echo /etc/X11/twm ;;
  235.     /usr/X11R6/lib/X11/xdm) echo /etc/X11/xdm ;;
  236.     /usr/X11R6/lib/X11/xinit) echo /etc/X11/xinit ;;
  237.     /usr/X11R6/lib/X11/xkb) echo /etc/X11/xkb ;;
  238.     /usr/X11R6/lib/X11/xserver) echo /etc/X11/xserver ;;
  239.     /usr/X11R6/lib/X11/xsm) echo /etc/X11/xsm ;;
  240.     /usr/bin/X11) echo ../X11R6/bin ;;
  241.     /usr/bin/rstartd) echo ../X11R6/bin/rstartd ;;
  242.     /usr/include/X11) echo ../X11R6/include/X11 ;;
  243.     /usr/lib/X11) echo ../X11R6/lib/X11 ;;
  244.     *) internal_error "maplink() called with unknown path \"$1\"" ;;
  245.   esac
  246. }
  247.  
  248. analyze_path () {
  249.   # given a supplied set of pathnames, break each one up by directory and do an
  250.   # ls -dl on each component, cumulatively; i.e.
  251.   # analyze_path /usr/X11R6/bin -> ls -dl /usr /usr/X11R6 /usr/X11R6/bin
  252.   # Thanks to Randolph Chung for this clever hack.
  253.  
  254.   #local f g
  255.  
  256.   while [ -n "$1" ]; do
  257.     reject_whitespace "$1"
  258.     _g=
  259.     message "Analyzing $1:"
  260.     for _f in $(echo "$1" | tr / \  ); do
  261.       if [ -e /$_g$_f ]; then
  262.         ls -dl /$_g$_f /$_g$_f.dpkg-* 2> /dev/null || true
  263.         _g=$_g$_f/
  264.       else
  265.         message "/$_g$_f: nonexistent; directory contents of /$_g:"
  266.         ls -l /$_g
  267.         break
  268.       fi
  269.     done
  270.     shift
  271.   done
  272. }
  273.  
  274. find_culprits () {
  275.   #local f p dpkg_info_dir possible_culprits smoking_guns bad_packages package \
  276.   #  msg
  277.  
  278.   reject_whitespace "$1"
  279.   message "Searching for overlapping packages..."
  280.   _dpkg_info_dir=/var/lib/dpkg/info
  281.   if [ -d $_dpkg_info_dir ]; then
  282.     if [ "$(echo $_dpkg_info_dir/*.list)" != "$_dpkg_info_dir/*.list" ]; then
  283.       _possible_culprits=$(ls -1 $_dpkg_info_dir/*.list | egrep -v \
  284.         "(xbase-clients|x11-common|xfs|xlibs)")
  285.       if [ -n "$_possible_culprits" ]; then
  286.         _smoking_guns=$(grep -l "$1" $_possible_culprits || true)
  287.         if [ -n "$_smoking_guns" ]; then
  288.           _bad_packages=$(printf "\\n")
  289.           for f in $_smoking_guns; do
  290.             # too bad you can't nest parameter expansion voodoo
  291.             p=${f%*.list}      # strip off the trailing ".list"
  292.             _package=${p##*/}   # strip off the directories
  293.             _bad_packages=$(printf "%s\n%s" "$_bad_packages" "$_package")
  294.           done
  295.           _msg=$(cat <<EOF
  296. The following packages appear to have file overlaps with the X.Org packages;
  297. these packages are either very old, or in violation of Debian Policy.  Try
  298. upgrading each of these packages to the latest available version if possible:
  299. for example, with the command "apt-get install".  If no newer version of a
  300. package is available, you will have to remove it; for example, with the command
  301. "apt-get remove".  If even the latest available version of the package has
  302. this file overlap, please file a bug against that package with the Debian Bug
  303. Tracking System.  You may want to refer the package maintainer to section 12.8
  304. of the Debian Policy manual.
  305. EOF
  306. )
  307.           message "$_msg"
  308.           message "The overlapping packages are: $_bad_packages"
  309.         else
  310.           message "no overlaps found."
  311.         fi
  312.       fi
  313.     else
  314.       message "cannot search; no matches for $_dpkg_info_dir/*.list."
  315.     fi
  316.   else
  317.     message "cannot search; $_dpkg_info_dir does not exist."
  318.   fi
  319. }
  320.  
  321. # we require a readlink command or shell function
  322. if ! which readlink > /dev/null 2>&1; then
  323.   message "The readlink command was not found.  Please install version" \
  324.           "1.13.1 or later of the debianutils package."
  325.   readlink () {
  326.     # returns what symlink in $1 actually points to
  327.     perl -e '$l = shift; exit 1 unless -l $l; $r = readlink $l; exit 1 unless $r; print "$r\n"' "$1"
  328.   }
  329. fi
  330.  
  331. check_symlink () {
  332.   # syntax: check_symlink symlink
  333.   #
  334.   # See if specified symlink points where it is supposed to.  Return 0 if it
  335.   # does, and 1 if it does not.
  336.   #
  337.   # Primarily used by check_symlinks_and_warn() and check_symlinks_and_bomb().
  338.  
  339.   #local symlink
  340.  
  341.   # validate arguments
  342.   if [ $# -ne 1 ]; then
  343.     usage_error "check_symlink() called with wrong number of arguments;" \
  344.                 "expected 1, got $#"
  345.     exit $SHELL_LIB_USAGE_ERROR
  346.   fi
  347.  
  348.   _symlink="$1"
  349.  
  350.   if [ "$(maplink "$_symlink")" = "$(readlink "$_symlink")" ]; then
  351.     return 0
  352.   else
  353.     return 1
  354.   fi
  355. }
  356.  
  357. check_symlinks_and_warn () {
  358.   # syntax: check_symlinks_and_warn symlink ...
  359.   #
  360.   # For each argument, check for symlink sanity, and warn if it isn't sane.
  361.   #
  362.   # Call this function from a preinst script in the event $1 is "upgrade" or
  363.   # "install".
  364.  
  365.   #local errmsg symlink
  366.  
  367.   # validate arguments
  368.   if [ $# -lt 1 ]; then
  369.     usage_error "check_symlinks_and_warn() called with wrong number of" \
  370.                 "arguments; expected at least 1, got $#"
  371.     exit $SHELL_LIB_USAGE_ERROR
  372.   fi
  373.  
  374.   while [ -n "$1" ]; do
  375.     _symlink="$1"
  376.     if [ -L "$_symlink" ]; then
  377.       if ! check_symlink "$_symlink"; then
  378.         observe "$_symlink symbolic link points to wrong location" \
  379.                 "$(readlink "$_symlink"); removing"
  380.         rm "$_symlink"
  381.       fi
  382.     elif [ -e "$_symlink" ]; then
  383.       _errmsg="$_symlink exists and is not a symbolic link; this package cannot"
  384.       _errmsg="$_errmsg be installed until this"
  385.       if [ -f "$_symlink" ]; then
  386.         _errmsg="$_errmsg file"
  387.       elif [ -d "$_symlink" ]; then
  388.         _errmsg="$_errmsg directory"
  389.       else
  390.         _errmsg="$_errmsg thing"
  391.       fi
  392.       _errmsg="$_errmsg is removed"
  393.       die "$_errmsg"
  394.     fi
  395.     shift
  396.   done
  397. }
  398.  
  399. check_symlinks_and_bomb () {
  400.   # syntax: check_symlinks_and_bomb symlink ...
  401.   #
  402.   # For each argument, check for symlink sanity, and bomb if it isn't sane.
  403.   #
  404.   # Call this function from a postinst script.
  405.  
  406.   #local problem symlink
  407.  
  408.   # validate arguments
  409.   if [ $# -lt 1 ]; then
  410.     usage_error "check_symlinks_and_bomb() called with wrong number of"
  411.                 "arguments; expected at least 1, got $#"
  412.     exit $SHELL_LIB_USAGE_ERROR
  413.   fi
  414.  
  415.   while [ -n "$1" ]; do
  416.     _problem=
  417.     _symlink="$1"
  418.     if [ -L "$_symlink" ]; then
  419.       if ! check_symlink "$_symlink"; then
  420.         _problem=yes
  421.         warn "$_symlink symbolic link points to wrong location" \
  422.              "$(readlink "$_symlink")"
  423.       fi
  424.     elif [ -e "$_symlink" ]; then
  425.       _problem=yes
  426.       warn "$_symlink is not a symbolic link"
  427.     else
  428.       _problem=yes
  429.       warn "$_symlink symbolic link does not exist"
  430.     fi
  431.     if [ -n "$_problem" ]; then
  432.       analyze_path "$_symlink" "$(readlink "$_symlink")"
  433.       find_culprits "$_symlink"
  434.       die "bad symbolic links on system"
  435.     fi
  436.     shift
  437.   done
  438. }
  439.  
  440. font_update () {
  441.   # run $UPDATECMDS in $FONTDIRS
  442.  
  443.   #local dir cmd shortcmd x_font_dir_prefix
  444.  
  445.   _x_font_dir_prefix="/usr/share/fonts/X11"
  446.  
  447.   if [ -z "$UPDATECMDS" ]; then
  448.     usage_error "font_update() called but \$UPDATECMDS not set"
  449.   fi
  450.   if [ -z "$FONTDIRS" ]; then
  451.     usage_error "font_update() called but \$FONTDIRS not set"
  452.   fi
  453.  
  454.   reject_unlikely_path_chars "$UPDATECMDS"
  455.   reject_unlikely_path_chars "$FONTDIRS"
  456.  
  457.   for _dir in $FONTDIRS; do
  458.     if [ -d "$_x_font_dir_prefix/$_dir" ]; then
  459.       for _cmd in $UPDATECMDS; do
  460.         if which "$_cmd" > /dev/null 2>&1; then
  461.           _shortcmd=${_cmd##*/}
  462.           observe "running $_shortcmd in $_dir font directory"
  463.       _cmd_opts=
  464.           if [ "$_shortcmd" = "update-fonts-alias" ]; then
  465.             _cmd_opts=--x11r7-layout
  466.           fi
  467.           if [ "$_shortcmd" = "update-fonts-dir" ]; then
  468.             _cmd_opts=--x11r7-layout
  469.           fi
  470.           if [ "$_shortcmd" = "update-fonts-scale" ]; then
  471.             _cmd_opts=--x11r7-layout
  472.           fi
  473.           $_cmd $_cmd_opts $_dir || warn "$_cmd $_cmd_opts $_dir" \
  474.                               "failed; font directory data may not" \
  475.                               "be up to date"
  476.         else
  477.           warn "$_cmd not found; not updating corresponding $_dir font" \
  478.                "directory data"
  479.         fi
  480.       done
  481.     else
  482.       warn "$_dir is not a directory; not updating font directory data"
  483.     fi
  484.   done
  485. }
  486.  
  487. remove_conffile_prepare () {
  488.   # syntax: remove_conffile_prepare filename official_md5sum ...
  489.   #
  490.   # Check a conffile "filename" against a list of canonical MD5 checksums.
  491.   # If the file's current MD5 checksum matches one of the "official_md5sum"
  492.   # operands provided, then prepare the conffile for removal from the system.
  493.   # We defer actual deletion until the package is configured so that we can
  494.   # roll this operation back if package installation fails.
  495.   #
  496.   # Call this function from a preinst script in the event $1 is "upgrade" or
  497.   # "install" and verify $2 to ensure the package is being upgraded from a
  498.   # version (or installed over a version removed-but-not-purged) prior to the
  499.   # one in which the conffile was obsoleted.
  500.  
  501.   #local conffile current_checksum
  502.  
  503.   # validate arguments
  504.   if [ $# -lt 2 ]; then
  505.     usage_error "remove_conffile_prepare() called with wrong number of" \
  506.                 "arguments; expected at least 2, got $#"
  507.     exit $SHELL_LIB_USAGE_ERROR
  508.   fi
  509.  
  510.   _conffile="$1"
  511.   shift
  512.  
  513.   # does the _conffile even exist?
  514.   if [ -e "$_conffile" ]; then
  515.     # calculate its checksum
  516.     _current_checksum=$(md5sum < "$_conffile" | sed 's/[[:space:]].*//')
  517.     # compare it to each supplied checksum
  518.     while [ -n "$1" ]; do
  519.       if [ "$_current_checksum" = "$1" ]; then
  520.         # we found a match; move the confffile and stop looking
  521.         observe "preparing obsolete conffile $_conffile for removal"
  522.         mv "$_conffile" "$_conffile.$THIS_PACKAGE-tmp"
  523.         break
  524.       fi
  525.       shift
  526.     done
  527.   fi
  528. }
  529.  
  530. remove_conffile_commit () {
  531.   # syntax: remove_conffile_commit filename
  532.   #
  533.   # Complete the removal of a conffile "filename" that has become obsolete.
  534.   #
  535.   # Call this function from a postinst script after having used
  536.   # remove_conffile_prepare() in the preinst.
  537.  
  538.   #local conffile
  539.  
  540.   # validate arguments
  541.   if [ $# -ne 1 ]; then
  542.     usage_error "remove_conffile_commit() called with wrong number of" \
  543.                 "arguments; expected 1, got $#"
  544.     exit $SHELL_LIB_USAGE_ERROR
  545.   fi
  546.  
  547.   _conffile="$1"
  548.  
  549.   # if the temporary file created by remove_conffile_prepare() exists, remove it
  550.   if [ -e "$_conffile.$THIS_PACKAGE-tmp" ]; then
  551.     observe "committing removal of obsolete conffile $_conffile"
  552.     rm "$_conffile.$THIS_PACKAGE-tmp"
  553.   fi
  554. }
  555.  
  556. remove_conffile_rollback () {
  557.   # syntax: remove_conffile_rollback filename
  558.   #
  559.   # Roll back the removal of a conffile "filename".
  560.   #
  561.   # Call this function from a postrm script in the event $1 is "abort-upgrade"
  562.   # or "abort-install" is  after having used remove_conffile_prepare() in the
  563.   # preinst.
  564.  
  565.   #local conffile
  566.  
  567.   # validate arguments
  568.   if [ $# -ne 1 ]; then
  569.     usage_error "remove_conffile_rollback() called with wrong number of" \
  570.                 "arguments; expected 1, got $#"
  571.     exit $SHELL_LIB_USAGE_ERROR
  572.   fi
  573.  
  574.   _conffile="$1"
  575.  
  576.   # if the temporary file created by remove_conffile_prepare() exists, move it
  577.   # back
  578.   if [ -e "$_conffile.$THIS_PACKAGE-tmp" ]; then
  579.     observe "rolling back removal of obsolete conffile $_conffile"
  580.     mv "$_conffile.$THIS_PACKAGE-tmp" "$_conffile"
  581.   fi
  582. }
  583.  
  584. replace_conffile_with_symlink_prepare () {
  585.   # syntax: replace_conffile_with_symlink_prepare oldfilename newfilename \
  586.   # official_md5sum ...
  587.   #
  588.   # Check a conffile "oldfilename" against a list of canonical MD5 checksums.
  589.   # If the file's current MD5 checksum matches one of the "official_md5sum"
  590.   # operands provided, then prepare the conffile for removal from the system.
  591.   # We defer actual deletion until the package is configured so that we can
  592.   # roll this operation back if package installation fails. Otherwise copy it
  593.   # to newfilename and let dpkg handle it through conffiles mechanism.
  594.   #
  595.   # Call this function from a preinst script in the event $1 is "upgrade" or
  596.   # "install" and verify $2 to ensure the package is being upgraded from a
  597.   # version (or installed over a version removed-but-not-purged) prior to the
  598.   # one in which the conffile was obsoleted.
  599.  
  600.   #local conffile current_checksum
  601.  
  602.   # validate arguments
  603.   if [ $# -lt 3 ]; then
  604.     usage_error "replace_conffile_with_symlink_prepare() called with wrong" \
  605.                 " number of arguments; expected at least 3, got $#"
  606.     exit $SHELL_LIB_USAGE_ERROR
  607.   fi
  608.  
  609.   _oldconffile="$1"
  610.   shift
  611.   _newconffile="$1"
  612.   shift
  613.  
  614.   remove_conffile_prepare "$_oldconffile" "$@"
  615.   # If $_oldconffile still exists, then md5sums didn't match.
  616.   # Copy it to new one.
  617.   if [ -f "$_oldconffile" ]; then
  618.     cp "$_oldconffile" "$_newconffile"
  619.   fi
  620.  
  621. }
  622.  
  623. replace_conffile_with_symlink_commit () {
  624.   # syntax: replace_conffile_with_symlink_commit oldfilename
  625.   #
  626.   # Complete the removal of a conffile "oldfilename" that has been
  627.   # replaced by a symlink.
  628.   #
  629.   # Call this function from a postinst script after having used
  630.   # replace_conffile_with_symlink_prepare() in the preinst.
  631.  
  632.   #local conffile
  633.  
  634.   # validate arguments
  635.   if [ $# -ne 1 ]; then
  636.     usage_error "replace_conffile_with_symlink_commit() called with wrong" \
  637.                 "number of arguments; expected 1, got $#"
  638.     exit $SHELL_LIB_USAGE_ERROR
  639.   fi
  640.  
  641.   _conffile="$1"
  642.  
  643.   remove_conffile_commit "$_conffile"
  644. }
  645.  
  646. replace_conffile_with_symlink_rollback () {
  647.   # syntax: replace_conffile_with_symlink_rollback oldfilename newfilename
  648.   #
  649.   # Roll back the replacing of a conffile "oldfilename" with symlink to
  650.   # "newfilename".
  651.   #
  652.   # Call this function from a postrm script in the event $1 is "abort-upgrade"
  653.   # or "abort-install" and verify $2 to ensure the package failed to upgrade
  654.   # from a version (or install over a version removed-but-not-purged) prior
  655.   # to the one in which the conffile was obsoleted.
  656.   # You should have  used replace_conffile_with_symlink_prepare() in the
  657.   # preinst.
  658.  
  659.   #local conffile
  660.  
  661.   # validate arguments
  662.   if [ $# -ne 2 ]; then
  663.     usage_error "replace_conffile_with_symlink_rollback() called with wrong" \
  664.                 "number of arguments; expected 2, got $#"
  665.     exit $SHELL_LIB_USAGE_ERROR
  666.   fi
  667.  
  668.   _oldconffile="$1"
  669.   _newconffile="$2"
  670.  
  671.   remove_conffile_rollback "$_oldconffile"
  672.   if [ -f "$_newconffile" ]; then
  673.     rm "$_newconffile"
  674.   fi
  675. }
  676.  
  677. run () {
  678.   # syntax: run command [ argument ... ]
  679.   #
  680.   # Run specified command with optional arguments and report its exit status.
  681.   # Useful for commands whose exit status may be nonzero, but still acceptable,
  682.   # or commands whose failure is not fatal to us.
  683.   #
  684.   # NOTE: Do *not* use this function with db_get or db_metaget commands; in
  685.   # those cases the return value of the debconf command *must* be checked
  686.   # before the string returned by debconf is used for anything.
  687.  
  688.   #local retval
  689.  
  690.   # validate arguments
  691.   if [ $# -lt 1 ]; then
  692.     usage_error "run() called with wrong number of arguments; expected at" \
  693.                 "least 1, got $#"
  694.     exit $SHELL_LIB_USAGE_ERROR
  695.   fi
  696.  
  697.   "$@" || _retval=$?
  698.  
  699.   if [ ${_retval:-0} -ne 0 ]; then
  700.     observe "command \"$*\" exited with status $_retval"
  701.   fi
  702. }
  703.  
  704. register_x_lib_dir_with_ld_so () {
  705.   # syntax: register_x_lib_dir_with_ld_so
  706.   #
  707.   # Configure the dynamic loader ld.so to search /usr/X11R6/lib for shared
  708.   # libraries.
  709.   #
  710.   # Call this function from the postinst script of a package that places a
  711.   # shared library in /usr/X11R6/lib, before invoking ldconfig.
  712.  
  713.   #local dir ldsoconf
  714.  
  715.   _dir="/usr/X11R6/lib"
  716.   _ldsoconf="/etc/ld.so.conf"
  717.  
  718.   # is the line not already present?
  719.   if ! fgrep -qsx "$_dir" "$_ldsoconf"; then
  720.     observe "adding $_dir directory to $_ldsoconf"
  721.     echo "$_dir" >> "$_ldsoconf"
  722.   fi
  723. }
  724.  
  725. deregister_x_lib_dir_with_ld_so () {
  726.   # syntax: deregister_x_lib_dir_with_ld_so
  727.   #
  728.   # Configure dynamic loader ld.so to not search /usr/X11R6/lib for shared
  729.   # libraries, if and only if no shared libaries remain there.
  730.   #
  731.   # Call this function from the postrm script of a package that places a shared
  732.   # library in /usr/X11R6/lib, in the event "$1" is "remove", and before
  733.   # invoking ldconfig.
  734.  
  735.   #local dir ldsoconf fgrep_status cmp_status
  736.  
  737.   _dir="/usr/X11R6/lib"
  738.   _ldsoconf="/etc/ld.so.conf"
  739.  
  740.   # is the line present?
  741.   if fgrep -qsx "$_dir" "$_ldsoconf"; then
  742.     # are there any shared objects in the directory?
  743.     if [ "$(echo "$_dir"/lib*.so.*.*)" = "$_dir/lib*.so.*.*" ]; then
  744.       # glob expansion produced nothing, so no shared libraries are present
  745.       observe "removing $_dir directory from $_ldsoconf"
  746.       # rewrite the file (very carefully)
  747.       set +e
  748.       fgrep -svx "$_dir" "$_ldsoconf" > "$_ldsoconf.dpkg-tmp"
  749.       _fgrep_status=$?
  750.       set -e
  751.       case $_fgrep_status in
  752.         0|1) ;; # we don't actually care if any lines matched or not
  753.         *) die "error reading \"$_ldsoconf\"; fgrep exited with status" \
  754.           "$_fgrep_status" ;;
  755.       esac
  756.       set +e
  757.       cmp -s "$_ldsoconf.dpkg-tmp" "$_ldsoconf"
  758.       _cmp_status=$?
  759.       set -e
  760.       case $_cmp_status in
  761.         0) rm "$_ldsoconf.dpkg-tmp" ;; # files are identical
  762.         1) mv "$_ldsoconf.dpkg-tmp" "$_ldsoconf" ;; # files differ
  763.         *) die "error comparing \"$_ldsoconf.dpkg-tmp\" to \"$_ldsoconf\";" \
  764.           "cmp exited with status $_cmp_status" ;;
  765.       esac
  766.     fi
  767.   fi
  768. }
  769.  
  770. make_symlink_sane () {
  771.   # syntax: make_symlink_sane symlink target
  772.   #
  773.   # Ensure that the symbolic link symlink exists, and points to target.
  774.   #
  775.   # If symlink does not exist, create it and point it at target.
  776.   #
  777.   # If symlink exists but is not a symbolic link, back it up.
  778.   #
  779.   # If symlink exists, is a symbolic link, but points to the wrong location, fix
  780.   # it.
  781.   #
  782.   # If symlink exists, is a symbolic link, and already points to target, do
  783.   # nothing.
  784.   #
  785.   # This function wouldn't be needed if ln had an -I, --idempotent option.
  786.  
  787.   # Validate arguments.
  788.   if [ $# -ne 2 ]; then
  789.     usage_error "make_symlink_sane() called with wrong number of arguments;" \
  790.       "expected 2, got $#"
  791.     exit $SHELL_LIB_USAGE_ERROR
  792.   fi
  793.  
  794.   # We could just use the positional parameters as-is, but that makes things
  795.   # harder to follow.
  796.   #local symlink target
  797.  
  798.   _symlink="$1"
  799.   _target="$2"
  800.  
  801.   if [ -L "$_symlink" ] && [ "$(readlink "$_symlink")" = "$_target" ]; then
  802.       observe "link from $_symlink to $_target already exists"
  803.   else
  804.     observe "creating symbolic link from $_symlink to $_target"
  805.     mkdir -p "${_target%/*}" "${_symlink%/*}"
  806.     ln -s -b -S ".dpkg-old" "$_target" "$_symlink"
  807.   fi
  808. }
  809.  
  810. migrate_dir_to_symlink () {
  811.   # syntax: migrate_dir_to_symlink old_location new_location
  812.   #
  813.   # Per Debian Policy section 6.5.4, "A directory will never be replaced by a
  814.   # symbolic link to a directory or vice versa; instead, the existing state
  815.   # (symlink or not) will be left alone and dpkg will follow the symlink if
  816.   # there is one."
  817.   #
  818.   # We have to do it ourselves.
  819.   #
  820.   # This function moves the contents of old_location, a directory, into
  821.   # new_location, a directory, then makes old_location a symbolic link to
  822.   # new_location.
  823.   #
  824.   # old_location need not exist, but if it does, it must be a directory (or a
  825.   # symlink to a directory).  If it is not, it is backed up.  If new_location
  826.   # exists already and is not a directory, it is backed up.
  827.   #
  828.   # This function should be called from a package's preinst so that other
  829.   # packages unpacked after this one --- but before this package's postinst runs
  830.   # --- are unpacked into new_location even if their payloads contain
  831.   # old_location filespecs.
  832.  
  833.   # Validate arguments.
  834.   if [ $# -ne 2 ]; then
  835.     usage_error "migrate_dir_to_symlink() called with wrong number of"
  836.                 "arguments; expected 2, got $#"
  837.     exit $SHELL_LIB_USAGE_ERROR
  838.   fi
  839.  
  840.   # We could just use the positional parameters as-is, but that makes things
  841.   # harder to follow.
  842.   local _new _old
  843.  
  844.   _old="$1"
  845.   _new="$2"
  846.  
  847.   # Is old location a symlink?
  848.   if [ -L "$_old" ]; then
  849.     # Does it already point to new location?
  850.     if [ "$(readlink "$_old")" = "$_new" ]; then
  851.       # Nothing to do; migration has already been done.
  852.       observe "migration of $_old to $_new already done"
  853.       return 0
  854.     else
  855.       # Back it up.
  856.       warn "backing up symbolic link $_old as $_old.dpkg-old"
  857.       mv -b "$_old" "$_old.dpkg-old"
  858.     fi
  859.   fi
  860.  
  861.   # Does old location exist, but is not a directory?
  862.   if [ -e "$_old" ] && ! [ -d "$_old" ]; then
  863.       # Back it up.
  864.       warn "backing up non-directory $_old as $_old.dpkg-old"
  865.       mv -b "$_old" "$_old.dpkg-old"
  866.   fi
  867.  
  868.   observe "migrating $_old to $_new"
  869.  
  870.   # Is new location a symlink?
  871.   if [ -L "$_new" ]; then
  872.     # Does it point the wrong way, i.e., back to where we're migrating from?
  873.     if [ "$(readlink "$_new")" = "$_old" ]; then
  874.       # Get rid of it.
  875.       observe "removing symbolic link $_new which points to $_old"
  876.       rm "$_new"
  877.     else
  878.       # Back it up.
  879.       warn "backing up symbolic link $_new as $_new.dpkg-old"
  880.       mv -b "$_new" "$_new.dpkg-old"
  881.     fi
  882.   fi
  883.  
  884.   # Does new location exist, but is not a directory?
  885.   if [ -e "$_new" ] && ! [ -d "$_new" ]; then
  886.     warn "backing up non-directory $_new as $_new.dpkg-old"
  887.     mv -b "$_new" "$_new.dpkg-old"
  888.   fi
  889.  
  890.   # Create new directory if it does not yet exist.
  891.   if ! [ -e "$_new" ]; then
  892.     observe "creating $_new"
  893.     mkdir -p "$_new"
  894.   fi
  895.  
  896.   # Copy files in old location to new location.  Back up any filenames that
  897.   # already exist in the new location with the extension ".dpkg-old".
  898.   observe "copying files from $_old to $_new"
  899.   if ! (cd "$_old" && cp -a -b -S ".dpkg-old" . "$_new"); then
  900.     die "error(s) encountered while copying files from $_old to $_new"
  901.   fi
  902.  
  903.   # Remove files at old location.
  904.   observe "removing $_old"
  905.   rm -r "$_old"
  906.  
  907.   # Create symlink from old location to new location.
  908.   make_symlink_sane "$_old" "$_new"
  909. }
  910.  
  911. # vim:set ai et sw=2 ts=2 tw=80:
  912.  
  913. # GOBSTOPPER: The X Strike Force shell library ends here.
  914.  
  915. case "$1" in
  916.   upgrade)
  917.     if dpkg --compare-versions "$2" le-nl "1:6.2.1+cvs.20050711-1"; then
  918.       if test -L /usr/lib/X11/locale && \
  919.          test "$(readlink /usr/lib/X11/locale)" = "../../share/X11/locale"; then
  920.     echo "Removing /usr/lib/X11/locale symlink."
  921.         rm /usr/lib/X11/locale
  922.         mkdir /usr/lib/X11/locale
  923.       fi
  924.     fi
  925.   ;;
  926. esac
  927.